﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

using XueMeng.BLL;
using XueMeng.Common;
using XueMeng.Model;

namespace UI
{
    public partial class Login : Form
    {

        /**
         * 
         * 
         * **/
        private readonly UserManager um = new UserManager();

        public Login()
        {
            InitializeComponent();
        }
        
        private void Login_Load(object sender, EventArgs e)
        {
            LoadImg();
            LoadEvent();
        }

        private void LoadEvent()
        {
            this.pictureBox1.Click += new EventHandler(pictureBox1_Click);
            this.button1.Click += new EventHandler(button1_Click);
            this.button2.Click += new EventHandler(button2_Click);
        }

        void button2_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        void button1_Click(object sender, EventArgs e)
        {
            if (String.IsNullOrEmpty(this.textBox1.Text)||String.IsNullOrEmpty(this.textBox2.Text))
            {
                MessageBox.Show("请输入用户名和密码");
                return;
            }
            if (this.textBox3.Text.ToUpper()!=this.pictureBox1.Tag.ToString().ToUpper())
            {
                MessageBox.Show("验证码不正确");
                Clean();
                return;
            }
            LoginAction();
        }

        private void LoginAction()
        {
            User obj = new User()
            {
                U_Name = this.textBox1.Text,
                U_Pwd = Encrypt.EncryptMd5(this.textBox2.Text),
                Enable = 0
            };
            Help.UserInfo.User = um.GetModel(obj);
            if (Help.UserInfo.User==null)
            {
                MessageBox.Show("登陆失败");
                Clean();
                return;
            }
            Main et = new Main();
            et.Show();
            this.Visible = false;
        }

        private void Clean()
        {
            this.textBox2.Text = "";
            this.textBox3.Text = "";
            LoadImg();
        }


        void pictureBox1_Click(object sender, EventArgs e)
        {
            LoadImg();
        }

        private void LoadImg()
        {
            this.pictureBox1.Tag = CreateCode.GetRandomCode(4);
            this.pictureBox1.Image = CreateCode.CreateImage(this.pictureBox1.Tag.ToString()) as Image;
        }

    }
}
